package com.authine.cloudpivot.ext.controller;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.facade.OrganizationFacade;
import com.authine.cloudpivot.engine.api.model.organization.DepartmentModel;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectQueryModel;
import com.authine.cloudpivot.engine.api.model.runtime.SelectionValue;
import com.authine.cloudpivot.engine.component.query.api.FilterExpression;
import com.authine.cloudpivot.engine.component.query.api.Page;
import com.authine.cloudpivot.engine.component.query.api.helper.PageableImpl;
import com.authine.cloudpivot.engine.component.query.api.helper.Q;
import com.authine.cloudpivot.ext.service.ZcOrgService;
import com.authine.cloudpivot.ext.service.ZcSyncOrgService;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.authine.cloudpivot.ext.controller.PagesUseController.getDepartments;

/**
 * @Author hxd
 * @Date 2022/3/17 14:56
 * @Description
 **/
@RestController
@RequestMapping("/api/zc/sync")
public class SyncOrgController extends BaseController {


    @Autowired
    ZcSyncOrgService zcSyncOrgService;
    @Autowired
    ZcOrgService zcOrgService;

    @RequestMapping("/fullAllSync")
    public ResponseResult fullAllSync() {
        return zcSyncOrgService.fullAllSync();
    }

    @RequestMapping("/syncSection")
    public ResponseResult syncSection(@RequestBody List<String> orgIds) {

        if (CollectionUtils.isEmpty(orgIds)) {
            return ResponseResult.builder().errmsg("部分组织id,不能为空").errcode(0l).build();
        }

        return zcSyncOrgService.syncSection(orgIds);
    }


    @GetMapping("/getEacUserInfo")
    public ResponseResult getEacUserInfo(@RequestParam String userId) {

        if (StringUtils.isEmpty(userId)) {
            return ResponseResult.builder().errmsg("userId,不能为空").errcode(0l).build();
        }

        UserModel user = getOrganizationFacade().getUser(userId);

        if (user == null) {
            return ResponseResult.builder().errmsg(String.format("用户不存在 userId=%s", userId)).errcode(0l).build();
        }

        Map<String, Object> userMap = zcOrgService.getUserSimpleInfoByUsername(user.getUsername());

//        if (MapUtils.isEmpty(userMap)) {
//            return ResponseResult.builder().errmsg(String.format("获取中车eac用户信息为空, username=", user.getUsername())).errcode(0l).build();
//        }

        JSONObject jsonObject = new JSONObject();

        //专业技术职务
        jsonObject.put("zyjszw", userMap.get("zyjszw"));
        //职业技能等级
        jsonObject.put("zyjndj", userMap.get("zyjndj"));
        //职位层级
        jsonObject.put("zwcj", userMap.get("zwcj"));
        //职业发展等级
        jsonObject.put("zyfzdj", userMap.get("zyfzdj"));
        //工号
        jsonObject.put("employeeNo", user.getUsername());
        //姓名
        jsonObject.put("name", user.getName());
        //工作时间
        jsonObject.put("gzsj", userMap.get("gzsj"));
        //职位
        jsonObject.put("zw", userMap.get("zw"));

        //中断时间
        jsonObject.put("zdsj", userMap.get("zdsj"));

        //2022-05-03 需求变动，将此注释
        //String route = MapUtils.getString(userMap, "route", "");
        //String[] split = route.split("/");
        //if (split.length > 0) {
        //    //单位
        //    jsonObject.put("dw",split[0]);
        //    //部门
        //    jsonObject.put("bm",split[split.length-1]);
        //}else {
        //    //单位
        //    jsonObject.put("dw",null);
        //    //部门
        //    jsonObject.put("bm",null);
        //}
        //2022-05-03 需求变动，将此注释


        BizObjectFacade bizObjectFacade = getBizObjectFacade();
        //2022-05-03 变动后的获取单位和部门的逻辑
        //xmb 所属项目部/分中心 默认一级部门
        OrganizationFacade organizationFacade = getDubboConfigService().getOrganizationFacade();
        ArrayList<String> departmentIdList = new ArrayList<>();
        getDepartments(organizationFacade, user.getDepartmentId(), departmentIdList);
        String xmbDepartmentId = "";
        if (departmentIdList.size() >= 2) {
            xmbDepartmentId = departmentIdList.get(departmentIdList.size() - 2);
        } else if (departmentIdList.size() == 1) {
            xmbDepartmentId = departmentIdList.get(0);
        }
        DepartmentModel xmbDepartment = organizationFacade.getDepartment(xmbDepartmentId, false);
        jsonObject.put("bm", xmbDepartment.getName());
        String dw = "";
        BizObjectQueryModel deptinfoQueryModel = new BizObjectQueryModel();
        deptinfoQueryModel.setSchemaCode("deptinfo");
        deptinfoQueryModel.setQueryCode("deptinfo");
        deptinfoQueryModel.setPageable(new PageableImpl(0, 20));
        FilterExpression.Item it = Q.it("stbm", FilterExpression.Op.Like, xmbDepartment.getId());
        deptinfoQueryModel.setFilterExpr(it);
        Page<BizObjectModel> deptInfoPage = bizObjectFacade.queryBizObjects(deptinfoQueryModel);
        if (deptInfoPage.getContent().size() > 0) {
            ArrayList<SelectionValue> xnbmList = (ArrayList<SelectionValue>) deptInfoPage.getContent().get(0).get("xnbm");
            SelectionValue xnbm = xnbmList.get(0);
            dw = xnbm.getName();
            jsonObject.put("dw", dw);
        } else {
            jsonObject.put("bm", "");
            jsonObject.put("dw", xmbDepartment.getName());
        }
        //结束

        BizObjectQueryModel queryModel = new BizObjectQueryModel();
        queryModel.setSchemaCode("jskgl");
        queryModel.setQueryCode("jskgl");
        queryModel.setPageable(new PageableImpl(0, 1));

        queryModel.setFilterExpr(Q.it("xm", FilterExpression.Op.Like, userId));
        Page<BizObjectModel> page = getBizObjectFacade().queryBizObjects(queryModel);

        if (page.getTotal() > 0) {
            //现任讲师级别
            jsonObject.put("xrjsjb", page.getContent().get(0).getString("jsdj"));
            //现任级别聘任时间
            jsonObject.put("prsj", page.getContent().get(0).getString("prsj"));
        } else {
            //现任讲师级别
            jsonObject.put("xrjsjb", null);
            //现任级别聘任时间
            jsonObject.put("prsj", null);
        }

        return getOkResponseResult(jsonObject, "成功");
    }


}
